package com.tong.ft_my.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.qihoo360.replugin.RePlugin;
import com.tong.ft_my.databinding.FragmentMyBinding;
import com.tong.lib_base.ft_login.ILoginService;
import com.tong.lib_base.base.AbcFragmentVM;
import com.tong.lib_base.ft_login.model.UserBean;
import com.tong.lib_base.ft_login.replugin.config.LoginPluginConfig;
import com.tong.lib_base.ft_login.service.impl.LoginImpl;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * author:liuzhitong
 * date:2022/3/2
 * description:
 */
public class FragmentMy extends AbcFragmentVM {
    FragmentMyBinding mbinding;
    private UserBroadcastReceiver mUserBroadcastReceiver;

    @Override
    public void initData() {
        registerUserReceiver();
    }

    @Override
    public void initViews(View view) {

        EventBus.getDefault().register(this);

    }

    @Override
    public View initBinding(@NonNull LayoutInflater inflater) {
        //        viewModel = new ViewModelProvider(this).get(ViewModelFragHome.class);
        mbinding = FragmentMyBinding.inflate(inflater);
        mbinding.setLifecycleOwner(getActivity());
        mbinding.setFragment(this);
//        mbinding.setViewModel(viewModel);
        return mbinding.getRoot();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unregisterUserReceiver();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveMsg(String message) {
        UserBean userInfo = LoginImpl.getInstance().getUserInfo();
        mbinding.textMessage.setText(userInfo.getUserAccount() + "  " + userInfo.getUserNickName() + "  " + userInfo.getUserPassword());
    }

    public void checkLogin() {
//        if(!LoginImpl.getInstance().hasLogin()){
//            LoginImpl.getInstance().login(getActivity(),"瞎传的参数");
        //通过插件化的aidl来识别是否登录
        //1/2.获取插件提供的binder对象
        IBinder binder =RePlugin.fetchBinder(LoginPluginConfig.PLUGIN_NAME,LoginPluginConfig.KEY_INTERFACE);
        Toast.makeText(getActivity(),"有异常="+(binder==null),Toast.LENGTH_LONG).show();
        if(binder == null){
            return;
        }
        //2/2.把ibinder对象强转成我们实现的类
        ILoginService iLoginService = ILoginService.Stub.asInterface(binder);
        try {
            if(iLoginService.hasLogin()){
                Toast.makeText(getActivity(),"已经登录了",Toast.LENGTH_LONG).show();
                UserBean userInfo = iLoginService.getUserInfo();
                mbinding.textMessage.setText(""+userInfo.getUserAccount()+userInfo.getUserNickName()+userInfo.getUserPassword());
            }else{
                Intent intent = RePlugin.createIntent("ft_login", LoginPluginConfig.PAGE.PAGE_LOGIN);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                RePlugin.startActivity(getActivity(), intent);
            }
        } catch (RemoteException e) {
            e.printStackTrace();
            Toast.makeText(getActivity(),"有异常="+e.getMessage(),Toast.LENGTH_LONG).show();
        }
//        }
    }

    private void updataLoginUI() {
        mbinding.textMessage.setText("HAHHAHAHAHAHAH");
    }
    private void registerUserReceiver() {
        if(mUserBroadcastReceiver == null){
            mUserBroadcastReceiver = new UserBroadcastReceiver();
        }
        getActivity().registerReceiver(mUserBroadcastReceiver,new IntentFilter(LoginPluginConfig.ACTION.LOGIN_SUCCESS_ACTION));
    }
    private void unregisterUserReceiver() {
        if(mUserBroadcastReceiver != null){
            getActivity().unregisterReceiver(mUserBroadcastReceiver);
        }

    }

    private class UserBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("广播","接收方");
            String action = intent.getAction();
            if (action.equals(LoginPluginConfig.ACTION.LOGIN_SUCCESS_ACTION)) {
                updataLoginUI();
            }
        }
    }
}
